<?php

require FCPATH . '/application/controllers/backend/backend.php';

class About extends Backend {

	public function __construct() {
		parent::__construct();
		$this->load->model('about_model', 'model');
	}

	public function index() {
		$this->load_js('backend_about');
		$this->rend('about', array(
			'l_position' => array(array('name' => '关于我们'))
		));
	}

	public function records() {
		$page = intval($this->input->get('page'));
		$page_size = intval($this->input->get('pageSize'));
		$page = $page ? $page : 1;
		$page_size = $page_size ? $page_size : 10;
		$offset = $page_size * ($page - 1);

		$conditions = array();
		$id = intval($this->input->get('id'));
		if (!empty($id)) {
			$conditions[] = array('where', 'id', $id);
		}

		$title = trim($this->input->get('title'));
		if (!empty($title)) {
			$conditions[] = array('or_like', 'title_zh', $title);
			$conditions[] = array('or_like', 'title_fr', $title);
			$conditions[] = array('or_like', 'title_en', $title);
		}

		$start = $this->input->get('start');
		if (!empty($start)) {
			$conditions[] = array('where', 'created_at >=', $start);
		}

		$end = $this->input->get('end');
		if (!empty($end)) {
			$conditions[] = array('where', 'created_at <=', $end);
		}

		$order_fields = array('menu_zh', 'menu_fr', 'menu_en', 'title_zh', 'title_fr', 'title_en', 'created_at');
		$order_field = intval($this->input->get('order_field'));
		$order_type = intval($this->input->get('order_type'));
		$orders = array();
		if ($order_field >= 0 && $order_field < count($order_fields)) {
			$orders = array(
				array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
			);
		}

		$this->json_back('XT001', '', array(
			'total' => $this->model->total($conditions),
			'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
		));
	}

	public function show() {
		$this->load->model('slide_show_model');

		$id = intval($this->uri->segment(4));

		$slide_show_conditions = array(
			array('where', 'type', 2),
			array('where', 'parent', $id)
		);

		if (!($about = $this->model->find($id))) {
			die('您所访问的资源不存在');
		}

		$data = array(
			'about' => $about,
			'slide_shows' => $this->slide_show_model->records(array(), $slide_show_conditions),
			'l_position' => array(
				array('name' => '关于我们'),
				array('name' => $about->menu_zh . ' / ' . $about->menu_fr . ' / ' . $about->menu_en)
			)
		);

		$this->load_js('backend_about_show');
		$this->load_css('backend_about_show');
		$this->rend('about_show', $data);
	}

	public function add() {
		$this->load_css('backend_about_form');
		$this->load_js('backend_about_form');
		$this->rend('about_add', array(
			'l_position' => array(
				array('name' => '关于我们'),
				array('name' => '添加子栏目')
			)
		));
	}

	public function edit() {
		$id = intval($this->uri->segment(4));
		$data = array(
			'about' => $this->model->find($id),
			'l_position' => array(
				array('name' => '关于我们'),
				array('name' => '编辑子栏目')
			)
		);
		$this->load_css('backend_about_form');
		$this->load_js('backend_about_form');
		$this->rend('about_edit', $data);
	}

	public function insert() {
		$data = array(
			'menu_zh' => trim($this->input->post('menu_zh')),
			'menu_fr' => trim($this->input->post('menu_fr')),
			'menu_en' => trim($this->input->post('menu_en')),
			'title_zh' => trim($this->input->post('title_zh')),
			'title_fr' => trim($this->input->post('title_fr')),
			'title_en' => trim($this->input->post('title_en')),
			'content_zh' => trim($this->input->post('content_zh')),
			'content_fr' => trim($this->input->post('content_fr')),
			'content_en' => trim($this->input->post('content_en')),
		);

		$this->validate($data);

		if (($id = $this->model->insert($data)) > 0) {
			$this->json_back('XT001', '添加成功', array('id' => $id));
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function update() {
		$id = intval($this->input->post('id'));
		$data = array(
			'menu_zh' => trim($this->input->post('menu_zh')),
			'menu_fr' => trim($this->input->post('menu_fr')),
			'menu_en' => trim($this->input->post('menu_en')),
			'title_zh' => trim($this->input->post('title_zh')),
			'title_fr' => trim($this->input->post('title_fr')),
			'title_en' => trim($this->input->post('title_en')),
			'content_zh' => trim($this->input->post('content_zh')),
			'content_fr' => trim($this->input->post('content_fr')),
			'content_en' => trim($this->input->post('content_en')),
		);

		$this->validate($data);

		if ($this->model->update($id, $data)) {
			$this->json_back('XT001', '更新成功', array('id' => $id));
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function delete() {
		$id = intval($this->input->get('id'));
		if ($this->model->delete($id)) {
			$this->json_back('XT001', '删除成功。');
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	private function validate($data) {
		if (empty($data['menu_zh'])) {
			$this->json_back('XT002', '请填写菜单（中文）');
		}

		if (empty($data['menu_fr'])) {
			$this->json_back('XT002', '请填写菜单（法文）');
		}

		if (empty($data['menu_en'])) {
			$this->json_back('XT002', '请填写菜单（英文）');
		}

		if (empty($data['title_zh'])) {
			$this->json_back('XT002', '请填写标题（中文）');
		}

		if (empty($data['title_fr'])) {
			$this->json_back('XT002', '请填写标题（法文）');
		}

		if (empty($data['title_en'])) {
			$this->json_back('XT002', '请填写标题（英文）');
		}

		if (empty($data['content_zh'])) {
			$this->json_back('XT002', '请填写内容（中文）');
		}

		if (empty($data['content_fr'])) {
			$this->json_back('XT002', '请填写内容（法文）');
		}

		if (empty($data['title_en'])) {
			$this->json_back('XT002', '请填写内容（英文）');
		}
	}

}
